﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Microsoft.EntityFrameworkCore;
using MVlog.Authorization;
using MVlog.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVlog.Application
{
    /// <summary>
    /// 评论
    /// </summary>
    [AbpAuthorize(AppPermissions.Pages_Comment)]

    public class CommentsAppService: AsyncCrudAppService<Comments,CommentsDto,long,GetCommentsInput,CreateCommentsInput,UpdateCommentsInput>, ICommentsAppService
    {
        private readonly IRepository<Article, long> _articles;
        private readonly IRepository<Video, long> _videos;
        public CommentsAppService(IRepository<Comments,long> repository,
            IRepository<Article,long> article,
            IRepository<Video,long> video
            )
           : base(repository)
        {
            _articles = article;
            _videos = video;
        }

        public override async Task<PagedResultDto<CommentsDto>> GetAll(GetCommentsInput input)
        {
            var articleIds =await _articles.GetAll().Where(o => o.CreatorUserId == AbpSession.UserId).Select(o => o.Id).ToListAsync();
            var videoIds=_videos.GetAll().Where(o => o.CreatorUserId == AbpSession.UserId).Select(o => o.Id).ToList();
            var data = Repository.GetAll().Where(o => articleIds.Contains(o.ArticleId.Value) || videoIds.Contains(o.VedioId.Value)).ToList();

            var count = data.Count();
            List<CommentsDto> list = new List<CommentsDto>();
            try
            {
                list =  ObjectMapper.Map<List<CommentsDto>>(data).AsQueryable().PageBy(input).ToList();
            }
            catch (Exception ex)
            { }

            return new PagedResultDto<CommentsDto>(count, list);
        }
    }
}
